/**
 iTextUploader
 (C) 2008 Asgl (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: iTextUploaderAppDelegate.m 28 2008-09-28 14:16:07Z myselftdev $ */

#import "iTextUploaderAppDelegate.h"

//#define UPLOAD_FILE "~/Library/SMS/sms.db"

@implementation iTextUploaderAppDelegate

@synthesize window;
@synthesize mainView;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{	
	window = [ [ UIWindow alloc ] initWithFrame:[[UIScreen mainScreen] bounds]];
	
    mainView = [ [ MainView alloc ] initWithFrame:[[UIScreen mainScreen] applicationFrame] parentObject:self];
	
	[window addSubview: mainView ];
    [window makeKeyAndVisible];
	
	preferencesData = [ [PreferencesData alloc] init];
	
	// We will create the preferencesView, uploadView and browseView objects later
	preferencesView = nil;
	uploadView = nil;
	browseView = nil;
	exportedView = nil;
	
	// Home page
	homeView = [ [HomeView alloc] init];
	[homeView setMainView:mainView preferencesData:preferencesData];
	[homeView isGoingToVisible];
	
	// The first selected tab is Home
	selectedTab = 1;
}

// Called when user selects an item on the tab bar
- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)selectedItem
{
	//NSLog(@"Selected tab: %d - %@", selectedItem.tag, selectedItem.title);
	
	if( (selectedItem.tag == 2 || selectedItem.tag == 4 || selectedItem.tag == 5) && !mainView.internetConnectionAvailable)
	{
		UITabBarItem *previousItem = [mainView getTabBarItem:selectedTab];
		[mainView.tabBar setSelectedItem:previousItem];
		[previousItem autorelease];
		
		UIAlertView *alertMessage = [ [UIAlertView alloc] initWithTitle:@"Not logged in" message:@"To view this section you must be connected to the Internet and logged in YouArchive.It" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alertMessage show];
		[alertMessage autorelease];
		
		return;
	}
	
	if( (selectedItem.tag == 2 || selectedItem.tag == 4) && (
		[[preferencesData getData:@"username"] isEqualToString:@""] ||
		[[preferencesData getData:@"password"] isEqualToString:@""]))
	{
		UITabBarItem* preferencesItem = [mainView getTabBarItem:3];
		[mainView.tabBar setSelectedItem:preferencesItem];
		[self tabBar:theTabBar didSelectItem:preferencesItem];
		[preferencesItem autorelease];
		
		UIAlertView *alertMessage = [ [UIAlertView alloc] initWithTitle:@"Set up first"	message:@"You must set up the application first, inserting your login data for YouArchive.It.\nIf you don't have an account yet, you can get one for free visiting http://youarchive.it with your browser." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alertMessage show];
		[alertMessage autorelease];
		
		return;
	}
	
	CGRect contentRect = CGRectMake(0.0, 0.0, 320.0, (460.0 - 93.0));
	
	if(selectedItem.tag == 1)
	{
		[preferencesView isGoingToHidden];
		[homeView isGoingToVisible];
		[mainView showNavBar];
		mainView.navItem.title = @"iTextUploader";
		
		[mainView.contentView bringSubviewToFront:homeView.homeWebView];
	}
	else if(selectedItem.tag == 2)
	{
		[homeView isGoingToHidden];
		[preferencesView isGoingToHidden];
		[mainView showNavBar];
		mainView.navItem.title = @"Import new messages";
		
		if(uploadView == nil)
		{
			uploadView = [ [UploadView alloc] initWithFrame:contentRect dataObject:preferencesData];
			
			[mainView.contentView addSubview:uploadView];
		}
		else
		{
			[mainView.contentView bringSubviewToFront:uploadView];
		}
	}
	else if(selectedItem.tag == 3)
	{
		[homeView isGoingToHidden];
		[mainView showNavBar];
		mainView.navItem.title = @"Preferences";
		
		if(preferencesView == nil)
		{
			preferencesView = [ [PreferencesView alloc] initWithFrame:contentRect dataObject:preferencesData homeViewObject:homeView mainViewObject:mainView];
			
			[mainView.contentView addSubview:preferencesView];
		}
		else
		{
			[mainView.contentView bringSubviewToFront:preferencesView];
		}
		
		[preferencesView isGoingToVisible];
	}
	else if(selectedItem.tag == 4)
	{
		[homeView isGoingToHidden];
		[preferencesView isGoingToHidden];
		[mainView hideNavBar];
		
		if(browseView == nil)
		{
			browseView = [ [BrowseView alloc] initWithFrame:contentRect mainView:mainView];
			
			[mainView.contentView addSubview:browseView];
		}
		else
		{
			[mainView.contentView bringSubviewToFront:browseView];
			[browseView loadHome];
		}
	}
	else if(selectedItem.tag == 5)
	{
		[homeView isGoingToHidden];
		[preferencesView isGoingToHidden];
		[mainView showNavBar];
		mainView.navItem.title = @"Restore messages";
		
		if(exportedView == nil)
		{
			exportedView = [ [ExportedView alloc] initWithFrame:contentRect dataObject:preferencesData];
			
			[mainView.contentView addSubview:exportedView];
		}
		else
		{
			[mainView.contentView addSubview:exportedView];
		}
	}
	
	selectedTab = selectedItem.tag;
}

- (void)dealloc
{
	[mainView release];
	[window release];
	[mainView release];
	[preferencesView release];
	[preferencesData release];
	[uploadView release];
	[browseView release];
	
	[super dealloc];
}

@end
